import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  AfterLoad
} from "typeorm"
import { User } from "./User";

import moment from "moment/moment"

moment.locale('zh-cn')

@Entity()
export class Exam {
  @PrimaryGeneratedColumn()
  id: number // 题库id

  @Column({ comment: "题库标题" })
  title: string // 题库标题

  @Column({ type: "longtext", nullable: true, comment: "题库描述" })
  description: string // 题库描述

  @Column({ comment: "题库存储地址" })
  fullPath: string // 题库在上传时的题库名

  @Column({ comment: "题库存储地址" })
  downloadUrl: string // 题库存储地址

  @Column({ default: false, comment: "是否完成" })
  isDone: boolean // 是否完成

  @Column({ comment: "题库作者id" })
  authorId: string // 题库作者id

  @Column({ comment: "需要写该题库的用户id" })
  writerId: string // 需要写该题库的用户id

  @ManyToOne(() => User, { createForeignKeyConstraints: false })
  author: User // 题库发布者

  @CreateDateColumn()
  createdAt: Date | string; // 文章发布时间

  @UpdateDateColumn()
  updatedAt: Date | string; // 文章更新时间

  @AfterLoad()
  formatDate() {
    this.createdAt = moment(this.createdAt).format('LL')
    this.updatedAt = moment(this.updatedAt).format('LL')
  }
}